
#include "LH096.h"
#include "lcd.h"


#define LH096_RESET_HIGH        HAL_GPIO_WritePin(PortReset, PinReset, GPIO_PIN_SET)
#define LH096_RESET_LOW         HAL_GPIO_WritePin(PortReset, PinReset, GPIO_PIN_RESET)


#define LH096_A0_HIGH           HAL_GPIO_WritePin(PortA0, PinA0, GPIO_PIN_SET)
#define LH096_A0_LOW            HAL_GPIO_WritePin(PortA0, PinA0, GPIO_PIN_RESET)


#define LH096_CS_HIGH           HAL_GPIO_WritePin(PortCS, PinCS, GPIO_PIN_SET)
#define LH096_CS_LOW            HAL_GPIO_WritePin(PortCS, PinCS, GPIO_PIN_RESET)


static uint8_t Height, Width;
static uint16_t offsetX, offsetY;
static SPI_HandleTypeDef *Hspi;
static GPIO_TypeDef *PortReset, *PortA0, *PortCS;
static uint16_t PinReset, PinA0, PinCS;



//========================================================
void LH096_writeCommand(uint8_t c)
{
  LH096_A0_LOW;
  LH096_CS_LOW;
  HAL_SPI_Transmit(Hspi, &c, 1, 0);
  LH096_CS_HIGH;
}


//========================================================
void LH096_writeData(uint8_t dat)
{
  LH096_A0_HIGH;
  LH096_CS_LOW;
  HAL_SPI_Transmit(Hspi, &dat, 1, 0);
  LH096_CS_HIGH;
}


void LH096_writeDataMany(uint8_t count, uint8_t * dat)
{
  LH096_A0_HIGH;
  LH096_CS_LOW;
  HAL_SPI_Transmit(Hspi, dat, count, 0);
  LH096_CS_HIGH;
}


//========================================================
void LH096_setRegion(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end)
{
  uint8_t x_params[] = {0x00, x_start + offsetX, 0x00, x_end + offsetX};
  uint8_t y_params[] = {0x00, y_start + offsetY, 0x00, y_end + offsetY};

  LH096_writeCommand(0x2A);
  LH096_writeDataMany(4, x_params);

  LH096_writeCommand(0x2B);
  LH096_writeDataMany(4, y_params);

  LH096_writeCommand(0x2C);
}

uint16_t LH096_getWidth(void)
{
  return Width;
}
uint16_t LH096_getHeight(void)
{
  return Height;
}


//========================================================
void LH096_init(SPI_HandleTypeDef* hspi, GPIO_TypeDef *portReset, uint16_t pinReset, GPIO_TypeDef *portA0, uint16_t pinA0, 
                  GPIO_TypeDef *portCs, uint16_t pinCs, uint8_t rotate)
{
  Hspi = hspi;
  PortReset = portReset;
  PinReset = pinReset;
  PortA0 = portA0;
  PinA0 = pinA0;
  PortCS = portCs;
  PinCS = pinCs;
  //------------------------------------ST7735R Reset Sequence-----------------------------------------// 
  LH096_RESET_HIGH;
  HAL_Delay(1);
  LH096_RESET_LOW;
  HAL_Delay(1);
  LH096_RESET_HIGH;
  HAL_Delay(1);

  //--------------------------------End ST7735R Reset Sequence --------------------------------------// 

  //--------------------------------End ST7735S Reset Sequence --------------------------------------// 
  LH096_writeCommand(0x11);                         //Sleep out 
  // delay(120);              //Delay 120ms 
  //------------------------------------ST7735S Frame Rate-----------------------------------------// 
  LH096_writeCommand(0xB1);
  LH096_writeData(0x05);
  LH096_writeData(0x3C);
  LH096_writeData(0x3C);
  LH096_writeCommand(0xB2);
  LH096_writeData(0x05);
  LH096_writeData(0x3C);
  LH096_writeData(0x3C);
  LH096_writeCommand(0xB3);
  LH096_writeData(0x05);
  LH096_writeData(0x3C);
  LH096_writeData(0x3C);
  LH096_writeData(0x05);
  LH096_writeData(0x3C);
  LH096_writeData(0x3C);

  //------------------------------------End ST7735S Frame Rate---------------------------------// 
  LH096_writeCommand(0xB4);                         //Dot inversion 
  LH096_writeData(0x03);

  //------------------------------------ST7735S Power Sequence---------------------------------// 
  LH096_writeCommand(0xC0);
  LH096_writeData(0xAB);
  LH096_writeData(0x0B);
  LH096_writeData(0x04);
  LH096_writeCommand(0xC1);
  LH096_writeData(0XC5);
  LH096_writeCommand(0xC2);
  LH096_writeData(0x0D);
  LH096_writeData(0x00);
  LH096_writeCommand(0xC3);
  LH096_writeData(0x8D);
  LH096_writeData(0x6A);
  LH096_writeCommand(0xC4);
  LH096_writeData(0x8D);
  LH096_writeData(0xEE);

  //---------------------------------End ST7735S Power Sequence-------------------------------------// 
  LH096_writeCommand(0xC5);                         //VCOM 
  LH096_writeData(0x0F);

  //------------------------------------ST7735S Gamma Sequence---------------------------------// 
  LH096_writeCommand(0xE0);
  LH096_writeData(0x07);
  LH096_writeData(0x0E);
  LH096_writeData(0x08);
  LH096_writeData(0x07);
  LH096_writeData(0x10);
  LH096_writeData(0x07);
  LH096_writeData(0x02);
  LH096_writeData(0x07);
  LH096_writeData(0x09);
  LH096_writeData(0x0F);
  LH096_writeData(0x25);
  LH096_writeData(0x36);
  LH096_writeData(0x00);
  LH096_writeData(0x08);
  LH096_writeData(0x04);
  LH096_writeData(0x10);
  LH096_writeCommand(0xE1);                         // negative gamma
  LH096_writeData(0x0A);
  LH096_writeData(0x0D);
  LH096_writeData(0x08);
  LH096_writeData(0x07);
  LH096_writeData(0x0F);
  LH096_writeData(0x07);
  LH096_writeData(0x02);
  LH096_writeData(0x07);
  LH096_writeData(0x09);
  LH096_writeData(0x0F);
  LH096_writeData(0x25);
  LH096_writeData(0x35);
  LH096_writeData(0x00);
  LH096_writeData(0x09);
  LH096_writeData(0x04);
  LH096_writeData(0x10);


  LH096_writeCommand(0xFC);
  LH096_writeData(0x80);


  //------------------------------------End ST7735S Gamma Sequence-----------------------------// 
  LH096_writeCommand(0x3A);                         //65k mode 
  LH096_writeData(0x05);
  LH096_writeCommand(0x36);
  switch (rotate)
  {
    case LCD_ROTAGE_0:
      LH096_writeData(0xC8);
      Width = 80;
      Height = 160;
      offsetX = 26;
      offsetY = 1;
    break;
    case LCD_ROTAGE_90:
      LH096_writeData(0x68);
      Width = 160;
      Height = 80;
      offsetX = 1;
      offsetY = 26;
    break;
    case LCD_ROTAGE_180:
      LH096_writeData(0x08);
      Width = 80;
      Height = 160;
      offsetX = 26;
      offsetY = 1;
    break;
    case LCD_ROTAGE_270:
      LH096_writeData(0xA8);
      Width = 160;
      Height = 80;
      offsetX = 1;
      offsetY = 26;
    break;
  }
  

  LH096_writeCommand(0x21);
  LH096_writeCommand(0x29);                         //Display on 

  LCD_init(LH096_writeDataMany, LH096_setRegion, LH096_getWidth, LH096_getHeight);
}



